package ui.finance;

import javax.swing.JDialog;

import tool.MyButton;
import tool.MyLabel;
import vo.AccountorVo;
import vo.UserVo;
import vo.Vo;
import blservice.finance.AccountorBLService;

import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.JButton;

import java.awt.Color;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.Toolkit;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.rmi.RemoteException;

public class AddAccountDialog extends JDialog{
	private static final long serialVersionUID = 7391883253070407565L;
	AccountorManagementPanel father;
	AccountorBLService bl;
	UserVo user;
	private JTextField name;
	private JTextField money;
	public AddAccountDialog(AccountorManagementPanel father,AccountorBLService bl,UserVo user) {
		this.setContentPane(new JPanel() {

			private static final long serialVersionUID = -8067534092982934270L;

			@Override
			public void paint(Graphics g) {
				this.setBackground(new Color(0, 0, 0, 0));
				Image background = Toolkit.getDefaultToolkit().getImage(
						"Graphics/framebackground.jpg");
				g.drawImage(background, 0, 0, null);
				super.paint(g);
			}
		});

		this.father=father;
		this.bl=bl;
		this.user=user;
		this.setBounds(200, 200, 400, 250);
		getContentPane().setLayout(null);
		
		JLabel label = new MyLabel("账户名称：");
		label.setBounds(70, 40, 91, 15);
		getContentPane().add(label);
		
		name = new JTextField();
		name.setBounds(167, 38, 143, 25);
		getContentPane().add(name);
		name.setColumns(10);
		
		JLabel label_1 = new MyLabel("金额：");
		label_1.setBounds(79, 95, 50, 15);
		getContentPane().add(label_1);
		
		money = new JTextField();
		money.setBounds(167, 93, 143, 25);
		getContentPane().add(money);
		money.setColumns(10);
		
		JButton confirm = new MyButton("Graphics/Button82-25.png");
		confirm.setText("确定");
		confirm.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				AccountorVo vo=new AccountorVo();
				vo.set名称(name.getText());
				vo.setOperator(user.getName());
				vo.setOperation("添加用户");
				try{
				vo.set金额(tool.LongStringTransformer.StringToLong(money.getText()));
				}catch(NumberFormatException e1){
					JOptionPane.showMessageDialog(AddAccountDialog.this, "金额格式错误");
					return;
				}
				Vo result;
				try {
					result = bl.addAccountor(vo);
					if(result.isSuccessed()){
						JOptionPane.showMessageDialog(AddAccountDialog.this, "添加成功");
						father.showAll();
						AddAccountDialog.this.dispose();
					}else{
						JOptionPane.showMessageDialog(AddAccountDialog.this, "添加失败\n"+result.getFailMessage());
					}
				} catch (RemoteException e1) {
					JOptionPane.showMessageDialog(AddAccountDialog.this, "无法连接到服务器\n"+e1.getMessage());
					e1.printStackTrace();
				}
				
			}
		});
		confirm.setBounds(82, 156, 82, 25);
		getContentPane().add(confirm);
		
		JButton cancel = new MyButton("Graphics/Button82-25.png");
		cancel.setText("取消");
		cancel.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				AddAccountDialog.this.dispose();
			}
		});
		cancel.setBounds(215, 156, 82, 25);
		getContentPane().add(cancel);
		setTitle("增加账户");
		setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
	}
}
